package com.wyp168.leetcode;

/**
 * @ClassName NumSubarrayBoundedMaxSolution
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2022/11/24 09:05
 */
public class NumSubarrayBoundedMaxSolution {
    public static void main(String[] args) {
        NumSubarrayBoundedMaxSolution solution = new NumSubarrayBoundedMaxSolution();
        int[] nums = {2, 1, 4, 3};
        int left = 2;
        int right = 3;
        System.out.println(solution.numSubarrayBoundedMax(nums, left, right));
    }

    public int numSubarrayBoundedMax(int[] nums, int left, int right) {
        int dist = 0;
        int count = 0;

        for(int i = 0; i < nums.length; i ++) {
            if (nums[i] >= left && nums[i] <= right) {
                dist ++;
                continue;
            }
            count += (dist)*(dist + 1) / 2;
            while (i < nums.length && (nums[i] > right || nums[i] < left)) {
                i ++;
            }
            if (i == nums.length) {
                return count;
            }
            dist = 1;
        }
        count += (dist)*(dist + 1) / 2;
        return count;
    }
}
